package cn.com.thingo.hyintelligent.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.app.App;
import cn.com.thingo.hyintelligent.util.Eyes;
import cn.com.thingo.hyintelligent.util.ToastUtil;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * Created by Administrator on 2017/10/9.
 */

public abstract class SimpleActivity extends SupportActivity {

    protected Activity mContext;
    private Unbinder mUnBinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(this.getLayout());
        mUnBinder = ButterKnife.bind(this);
        mContext = this;
        Eyes.setStatusBarColor(this, ContextCompat.getColor(this, R.color.gray_12));
        onViewCreated();
        App.getInstance().addActivity(this);
        try {
            initEventAndData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void setToolBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressedSupport();
            }
        });
    }

    protected void onViewCreated() {

    }

    @Override
    protected void onDestroy() {
        App.getInstance().removeActivity(this);
        super.onDestroy();
        mUnBinder.unbind();
    }

    public void showQX(final Context context, int content) {
        new MaterialDialog.Builder(context)
                .title("prompt")
                .content(content).positiveText("设置").negativeText("no")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        startAppSettings();
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        ToastUtil.show("未授予必要的权限，部分功能不可用！");
                    }
                }).show();
    }

    public void startAppSettings() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }

    protected abstract int getLayout();

    protected abstract void initEventAndData() throws Exception;
}
